package cn.pn.project.server;

import cn.pn.project.mapper.user.AttentionDao;
import cn.pn.project.pojo.user.Attention;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("attention")
public class AttentionService {
    @Autowired
    private AttentionDao attentionDao;

    /**
     * 添加用户关注商品的关系数据
     *
     * @param attention 主键id 不能有   userid 和商品id 必须有
     * @return 是否添加成功
     * @throws RuntimeException 运行时异常
     * @throws SQLException sql异常
     */
    @PostMapping("/add")
    public boolean addAttention(@RequestBody Attention attention) throws RuntimeException, SQLException {
        if(attention==null || attention.getAttentionId()!=null)
            return false;
        return attentionDao.insertAttention(attention) == 1;
    }

    /**
     * 查询关系数据
     * @param attention userId status 这两个必须有
     * @return 关系数据数据集合
     * @throws RuntimeException 运行时异常
     * @throws SQLException sql异常
     */
    @PostMapping("/find")
    public List<Attention> findAttentionByCommnuitStart(@RequestBody Attention attention) throws RuntimeException, SQLException {
        //判断参数是否符合要求
        if (attention == null || (attention.getUserId() == null || attention.getUserId().equals(0)) ) {
            return null;
        }
        return attentionDao.getAllAttention(attention);

    }

    /**
     * 删除关系数据
     * @param attention  attentionId userId commodityId 其他的不需要
     * @return
     * @throws RuntimeException 运行时异常
     * @throws SQLException sql异常
     */
    @PostMapping("/remove")
    public boolean removeAttention(@RequestBody Attention attention) throws RuntimeException, SQLException {
        Attention attention1=attentionDao.findAttention(attention);
        if(attention1==null)
            return false;
         return attentionDao.delAttention(attention.getAttentionId()) == 1;

    }

    /**
     * 获取指定用户的指定商品的关系数据
     *
     * @param attention userId commodityId
     * @return 返回单独（一条）的关系数据
     * @throws RuntimeException 运行时异常
     * @throws SQLException sql异常
     */
    @PostMapping("/findAttention")
    public Attention findAttention(@RequestBody Attention attention) throws RuntimeException, SQLException  {
        return attentionDao.findAttention(attention);
    }
}
